package cl.mt.util.servlet.comun;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import cl.mt.data.DataCiudad;
import cl.mt.data.DataRegion;
import cl.mt.dto.DTOCiudad;
import cl.mt.dto.DTORegion;

/**
 * Servlet implementation class CiudadServlet
 */
public class CiudadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	final Logger log = LogManager.getLogger(this.getClass());   
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CiudadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		log.debug("en region servlet");
		
		//regionId
		int id = Integer.parseInt(request.getParameter("regionId"));
		
		DataCiudad dtaCiudad = new DataCiudad();
		Collection<DTOCiudad> collCiudad = dtaCiudad.findByRegion(id);
		 response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println("<option value=''>Seleccione </option>");
		try{
			for(DTOCiudad ciudad : collCiudad){
				 out.printf("<option value='%1s'>%2s</option>", ciudad.getId(), ciudad.getDescripcion());
		            
			}
		}finally {
            out.close();
		}
	}

}
